<?php

import("@.Org.Utils");


/**
 * 取得用户等级数组,按用户级别排序
 * @param   bool      $is_special      是否只显示特殊会员组
 * @return  array     rank_id=>rank_name
 */
function get_rank_list($is_special = false){
    $rank_list 		= 	array();
    $sql 			= 	'SELECT rank_id, rank_name, min_points FROM ' . 'ybr_user_rank';
    if ($is_special){
        $sql 		.= 	' WHERE special_rank = 1';
    }
    $sql 			.= 	' ORDER BY min_points';

    $res 			=	M()->query($sql);
	foreach($res as $row){
		$rank_list[$row['rank_id']] = $row['rank_name'];
	}
    return $rank_list;
}

/**
 * 初始化会员数据整合类
 *
 * @access  public
 * @return  object
 */
function &init_users(){
    $set_modules = false;
    static $cls = null;
    if ($cls != null){
        return $cls;
    }
    
    import("@.Plugs.Ecshop");    
    $cfg 	= 	unserialize($GLOBALS['_CFG']['integrate_config']);   
    $cls 	= 	new $GLOBALS['_CFG']['integrate_code']($cfg);
    return $cls;
}

/**
 * 取得帐户明细
 * @param   int     $user_id    用户id
 * @param   string  $account_type   帐户类型：空表示所有帐户，user_money表示可用资金，
 *                  frozen_money表示冻结资金，rank_points表示等级积分，pay_points表示消费积分
 * @return  array
 */
function get_sql_accountlist($user_id, $account_type = ''){
	$CommonModel	=	new CommonModel();
    /* 检查参数 */
    $where 			= 	" WHERE user_id = '$user_id' ";
    if (in_array($account_type, array('user_money', 'frozen_money', 'rank_points', 'pay_points'))){
        $where 		.= 	" AND $account_type <> 0 ";
    }

    /* 初始化分页参数 */
    $filter = array(
        'user_id'       => $user_id,
        'account_type'  => $account_type
    );

    /* 查询记录 */
    $sql 					= 	"SELECT * FROM " . 'ybr_account_log' . $where . " ORDER BY log_id DESC";
    return $sql;
}

/**
 * 订单用户信息
 * @param   int     $user_id    用户id
 * @return  array   
 * 
 */
function user_info($user_id){
	
	$CommonModel	=	new CommonModel();
	
    $sql 			= 	"SELECT * FROM " . ' ybr_users ' . " WHERE user_id = '$user_id'";
    $user 			= 	$CommonModel->getRow($sql);

    unset($user['question']);
    unset($user['answer']);

    /* 若用户存在,格式化用户的财产和冻结的财产 */
    if ($user){
        $user['formated_user_money'] = price_format($user['user_money'], false);
        $user['formated_frozen_money'] = price_format($user['frozen_money'], false);
    }
    return $user;
}

/**
 *
 *
 * @access  public
 * @param
 *
 * @return void
 */
function get_sql_msg_list(){

	$CommonModel		  =		new CommonModel();	
	
    /* 过滤条件 */
    $filter['keywords']   = 	empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
    if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
        $filter['keywords'] = 	$filter['keywords'];
    }
    $filter['msg_type']   = 	isset($_REQUEST['msg_type']) ? intval($_REQUEST['msg_type']) : -1;
    $filter['sort_by']    = 	empty($_REQUEST['sort_by']) ? 'f.msg_id' : trim($_REQUEST['sort_by']);
    $filter['sort_order'] = 	empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

    $where 				  =	 	'';
    if ($filter['keywords']){
        $where .= " AND f.msg_title LIKE '%" . Utils::mysql_like_quote($filter['keywords']) . "%' ";
    }
    if ($filter['msg_type'] != -1){
        $where .= " AND f.msg_type = '$filter[msg_type]' ";
    }

    $sql 					= 	"SELECT count(*) FROM " . 'ybr_feedback' . " AS f" .
           						" WHERE parent_id = '0' " . $where;
    $filter['record_count'] = 	$CommonModel->getOne($sql);

  

    $sql = "SELECT f.msg_id, f.user_name, f.msg_title, f.msg_type, f.order_id, f.msg_status, f.msg_time, f.msg_area, COUNT(r.msg_id) AS reply " .
            "FROM " . 'ybr_feedback' . " AS f ".
            "LEFT JOIN " . 'ybr_feedback' . " AS r ON r.parent_id=f.msg_id ".
            "WHERE f.parent_id = 0 $where " .
            "GROUP BY f.msg_id ".
            "ORDER by $filter[sort_by] $filter[sort_order] ";
    
    return $sql;
}

/**
 * 获得留言的详细信息
 *
 * @param   integer $id
 *
 * @return  array
 */
function get_feedback_detail($id){

	$CommonModel	=	new CommonModel();
	
    $sql 			= 	"SELECT T1.*, T2.msg_id AS reply_id, T2.user_name  AS reply_name, u.email AS reply_email, ".
                		"T2.msg_content AS reply_content , T2.msg_time AS reply_time, T2.user_name AS reply_name ".
            			"FROM " . 'ybr_feedback' . " AS T1 ".
            			"LEFT JOIN " . 'ybr_admin_user' . " AS u ON u.user_id='" .$_SESSION['admin_id']. "' ".
            			"LEFT JOIN " . 'ybr_feedback' . " AS T2 ON T2.parent_id=T1.msg_id ".
            			"WHERE T1.msg_id = '$id'";    
   	//echo $sql;    
    $msg 					= 	$CommonModel->GetRow($sql);
    if ($msg){
        $msg['msg_time']   = 	local_date($GLOBALS['_CFG']['time_format'], $msg['msg_time']);
        $msg['reply_time'] = 	local_date($GLOBALS['_CFG']['time_format'], $msg['reply_time']);
    }
    return $msg;
}
    

/**
 *
 *
 * @access  public
 * @param
 *
 * @return void
 */
function get_sql_account_list(){
        /* 过滤列表 */
        $filter['user_id'] 			= 	!empty($_REQUEST['user_id']) ? intval($_REQUEST['user_id']) : 0;
        $filter['keywords'] 		= 	empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
        if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
            $filter['keywords'] 	= 	$filter['keywords'];
        }
        $filter['process_type'] 	= 	isset($_REQUEST['process_type']) ? intval($_REQUEST['process_type']) : -1;
        $filter['payment'] 			= 	empty($_REQUEST['payment']) ? '' : trim($_REQUEST['payment']);
        $filter['is_paid'] 			= 	isset($_REQUEST['is_paid']) ? intval($_REQUEST['is_paid']) : -1;
        $filter['sort_by'] 			= 	empty($_REQUEST['sort_by']) ? 'add_time' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] 		= 	empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['start_date'] 		= 	empty($_REQUEST['start_date']) ? '' : local_strtotime($_REQUEST['start_date']);
        $filter['end_date'] 		= 	empty($_REQUEST['end_date']) ? '' : (local_strtotime($_REQUEST['end_date']) + 86400);

        $where 						= 	" WHERE 1 ";
        if ($filter['user_id'] > 0){
            $where 					.=	" AND ua.user_id = '$filter[user_id]' ";
        }
        if ($filter['process_type'] != -1){
            $where 					.= 	" AND ua.process_type = '$filter[process_type]' ";
        }
        else{
            $where 					.= 	" AND ua.process_type " . db_create_in(array(SURPLUS_SAVE, SURPLUS_RETURN));
        }
        if ($filter['payment']){
            $where 					.= 	" AND ua.payment = '$filter[payment]' ";
        }
        if ($filter['is_paid'] != -1){
            $where 					.= 	" AND ua.is_paid = '$filter[is_paid]' ";
        }

        if ($filter['keywords']){
            $where 					.= 	" AND u.user_name LIKE '%" . Utils::mysql_like_quote($filter['keywords']) . "%'";
            $sql 					= 	"SELECT COUNT(*) FROM " .'ybr_user_account' . " AS ua, " . 'ybr_users' . " AS u " . $where;
        }
        /*　时间过滤　*/
        if (!empty($filter['start_date']) && !empty($filter['end_date'])){
            $where 					.= 	"AND paid_time >= " . $filter['start_date']. " AND paid_time < '" . $filter['end_date'] . "'";
        }
        
        /* 查询数据 */
        $sql  						= 	'SELECT ua.*, u.user_name FROM ' .
            							'ybr_user_account' . ' AS ua LEFT JOIN ' .
            							'ybr_users' . ' AS u ON ua.user_id = u.user_id'.
            							$where . "ORDER by " . $filter['sort_by'] . " " .$filter['sort_order'];

    	return $sql;
}







